﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Globalization;
public partial class Main_Reports_Inventory_ConsumptionTrendItemWise : System.Web.UI.Page
{
    Datautility DU = new Datautility(); 
    DataTable DtItemConsumption = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void GetItemWiseConsuption()
    {
        if (ddlYear.SelectedValue != "0")
        {
            lblperiod.Text = "Period :"+ddlYear.SelectedValue+" Years";
            SqlParameter[] Param = { new SqlParameter("@year",Convert.ToInt32(ddlYear.SelectedValue))};
            DtItemConsumption = DU.getdatausingprocedure("IVR_RPT_CONSUMPTIONTRENDITEMWISE_REPORT",Param);
            if (DtItemConsumption.Rows.Count > 0)
            {
                grdCTIW.DataSource = DtItemConsumption;                
                grdCTIW.DataBind();
                grdCTIW.HeaderRow.Cells[11].Text = "Consumption On Jan " + ddlYear.SelectedValue;
                grdCTIW.HeaderRow.Cells[12].Text = "Consumption On Feb " + ddlYear.SelectedValue;
                grdCTIW.HeaderRow.Cells[13].Text = "Consumption On Mar " + ddlYear.SelectedValue;
                grdCTIW.HeaderRow.Cells[14].Text = "Consumption On Apr " + ddlYear.SelectedValue;
                grdCTIW.HeaderRow.Cells[15].Text = "Consumption On May " + ddlYear.SelectedValue;
                grdCTIW.HeaderRow.Cells[16].Text = "Consumption On Jun " + ddlYear.SelectedValue;
                grdCTIW.HeaderRow.Cells[17].Text = "Consumption On Jul " + ddlYear.SelectedValue;
                grdCTIW.HeaderRow.Cells[18].Text = "Consumption On Aug " + ddlYear.SelectedValue;
                grdCTIW.HeaderRow.Cells[19].Text = "Consumption On Sep " + ddlYear.SelectedValue;
                grdCTIW.HeaderRow.Cells[20].Text = "Consumption On Oct " + ddlYear.SelectedValue;
                grdCTIW.HeaderRow.Cells[21].Text = "Consumption On Nov " + ddlYear.SelectedValue;
                grdCTIW.HeaderRow.Cells[22].Text = "Consumption On Dec " + ddlYear.SelectedValue;
                lblRecordNo.Text = grdCTIW.Rows.Count.ToString() + " Record(s)";
                lblRecordNo.ForeColor = System.Drawing.Color.Green;                
            }
            else
            {
                lblRecordNo.Text = "No Record(s) Found....";
                lblRecordNo.ForeColor = System.Drawing.Color.Red;
            }            
        }
    }
    protected void btngetreport_Click(object sender, EventArgs e)
    {
        GetItemWiseConsuption();
    }
    protected void ImgBtnExcel_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "ConsumptionTrendItemWise.xls"));
            Response.ContentType = "application/ms-excel";
            grdCTIW.ShowFooter = false;
            grdCTIW.AllowPaging = false;
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            grdCTIW.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
        }
        catch (Exception ex)
        {
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        grdCTIW.Visible = true;
        GetItemWiseConsuption();
        string Filter = "";
        if (ddlColumnname.SelectedValue == "1")//for Item Code
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "ITMCODE like'%" + txtValue.Text + "%'");
            grdCTIW.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "2")//for Item name
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "ITMDESC Like'%" + txtValue.Text + "%'");
            grdCTIW.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "3")//For AltItemCode
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "ITMALTDESC Like'%" + txtValue.Text + "%'");
            grdCTIW.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "4")//For Binref
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "BINREF Like'%" + txtValue.Text + "%'");
            grdCTIW.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "5")//For MPn
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "MPNNO Like'%" + txtValue.Text + "%'");
            grdCTIW.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "6")//Project NAme
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "projectname Like'%" + txtValue.Text + "%'");
            grdCTIW.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "7") //For Customer
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "Customer Like'%" + txtValue.Text + "%'");
            grdCTIW.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "8") //For Re-Order Qty
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "REORDERQTY='" + txtValue.Text + "'");
            grdCTIW.AllowPaging = false;
        }
        else
        {
            GetItemWiseConsuption();
        }

        DataRow[] Dr = DtItemConsumption.Select(Filter);
        DataTable Tempdt = new DataTable();
        Tempdt = DtItemConsumption.Clone();
        foreach (DataRow drNew in Dr)
        {
            Tempdt.ImportRow(drNew);
        }
        grdCTIW.DataSource = Tempdt;
        grdCTIW.DataBind();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }
}